No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without permission in writing from the publisher. However, you are permitted to make copies of this work, printed or otherwise, as long as said copies are for your personal use only.
Introduction
------------
If you've got a tip or a trick that you think would benefit other Open Mike readers, please share it with us. Readers whose tip or trick we use will receive a free issue of Open Mike. Such a deal!
A reader asks us how to deal with a particularly hairy situation, so we'll dedicate this column to tackling the problem.
The question is, how do you find out the name of a file that you've just downloaded? Though we wish that MicroPhone had some sort of built-in way of getting around this problem, it's something that can be done from scripting. So, here is an answer of sorts.
The basis of finding out the name of the file that was just downloaded is simple. First, take a snapshot of the world before the download. Then, take a snapshot after the download. Compare the two snapshots and the difference will be the new file. Simple enough, right?
Set Variable fname from Expression "''"
Set Variable one_dir from Expression "GetDir(ReceivePath,false)"
Receive File ZMODEM MacBinary "'dummy name'"
Set Variable two_dir from Expression "GetDir(ReceivePath,false)"
Set Variable c from Expression "0"
Set Variable m from Expression "IndexMax(two_dir)"
While Expression "c < m"
Set Variable c from Expression "c + 1"
If Expression "one_dir[c] <> two_dir[c]"
Remark "--- this must be the file!"
Set Variable fname from Expression "two_dir[c]"
Set Variable c from Expression "m"
End If
End While
If Expression "Length(fname) > 0"
Alert OK "'The downloaded file is called ' & fname & '.'"
End If
In order to find out where the file is going to be downloaded, use the ReceivePath function. This will tell you the directory into which the file will go. Grab a list of files in this directory and store it in a variable. Then, go ahead with the file transfer. After the file transfer, grab a new list of files in this directory. Now, all you have to do is compare the directory contents one-by-one until you don't get a match. When this happens, you know that you have the name of the new file.
There is one problem with the above script: What if the user has downloaded a batch of files? It's only going to return the name of the first different file it finds. What do we do in a situation like this? The best way to deal with this situation is to not assume that only one file is going to be different. Instead, build a third list of files, ones that do not exist in both the first and second lists. This is easier than it sounds. Take a look at the following lists:
Banana Banana
Cherry Cherry
Grapefruit Doughnut
Pineapple Grapefruit
Jackelope
Pineapple
What you want to do is create a third list that only contains:
Doughnut
Jackelope
So, the sixty eight thousand dollar question is, how do you generate this list? The answer to the sixty eight thousand dollar question is, rewrite the above script to accomodate such a situation. You need to create a loop within a loop, and for every element in the second list, check it against each element in the first list. If there is no match, write the element to a third list.
Delete Variable fnames
Set Variable one_dir from Expression "GetDir(ReceivePath,false)"
Receive File ZMODEM MacBinary "'dummy name'"
Set Variable two_dir from Expression "GetDir(ReceivePath,false)"
Set Variable c from Expression "0"
Set Variable m from Expression "IndexMax(two_dir)"
While Expression "c < m"
Set Variable c from Expression "c + 1"
Set Variable x from Expression "0"
Set Variable y from Expression "IndexMax(one_dir)"
Set Variable match from Expression "false"
While Expression "x < y"
Set Variable x from Expression "x + 1"
If Expression "two_dir[c] = one_dir[x]"
Remark "--- this is an old file"
Set Variable match from Expression "true"
End If
End While
If Expression "not match"
Remark "--- this must be a new file"
Set Variable z from Expression "IndexMax(fnames) + 1"
Set Variable fnames[z] from Expression "two_dir[c]"
End If
End While
If Expression "IndexMax(fnames) > 0"
Set Variable temp from Expression "ChooseOne(fnames,'New Files:','OK')"